package org.hzero.order.api.controller.v1;

/**
 * @author xiuiqng.Yang-Estelle-양수청
 * @date 2019/8/7 19:34
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.order.app.service.HeaderService;
import org.hzero.order.config.SwaggerTags;
import org.hzero.order.domain.entity.Header;
import org.hzero.order.domain.repository.HeaderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;

/**
 * API接口  新增订单与修改订单与数据库表对应分别请求订单头和订单行接口
 */
@Api(tags = SwaggerTags.HEADER)
@RestController("headerController.v1")
@RequestMapping("/v1/{organizationId}/headers")
public class HeaderController extends BaseController {

    @Autowired
    private HeaderService headerService;
    @Autowired
    private HeaderRepository headerRepository;

    @ApiOperation(value = "新增订单头信息")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PostMapping
    public ResponseEntity<Header> createOrderHeader(@RequestBody Header header){
        // 简单数据校验
        this.validObject(header);
        return Results.success(headerService.insertHeader(header));
    }

    @ApiOperation(value = "修改订单头信息")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PutMapping
    public ResponseEntity<Header> updateOrderHeader(@RequestBody Header header){
        // 简单数据校验
        this.validObject(header);
        return Results.success(headerService.updateHeader(header));
    }

    @ApiOperation(value = "提供删除订单头接口（页面一般为禁用）")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @DeleteMapping
    public ResponseEntity<Header> deleteOrderHeader(@RequestBody Header header){
        // 简单数据校验
        this.validObject(header);
        return Results.success(headerService.deleteHeader(header));
    }

    @ApiOperation(value = "根据订单编号查询订单头")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @GetMapping
    public ResponseEntity<Page<Header>> queryHeaderList(
            String orderNumber,
            PageRequest pageRequest){
        Header header = new Header();
        header.setOrderNumber(orderNumber);
        return Results.success(headerRepository.pageHeader(header, pageRequest));
    }
}
